import { defineConfig } from "vite"
import path from "path"
import react from "@vitejs/plugin-react"
import { visualizer } from "rollup-plugin-visualizer"
import legacy from "@vitejs/plugin-legacy"

const isProduction = process.env.NODE_ENV === "production"

export default defineConfig({
    base: "/",
    resolve: {
        alias: {
            "@": path.resolve(process.cwd(), "src/")
        }
    },
    plugins: [react(), legacy({ targets: ["defaults", "not IE 11"] }), visualizer({ open: isProduction })],
    build: {
        outDir: "dist",
        minify: isProduction ? "terser" : false,
        emptyOutDir: true,
        assetsInlineLimit: 4096,
        cssCodeSplit: true,
        reportCompressedSize: true,
        chunkSizeWarningLimit: 500,
        rollupOptions: {
            output: {
                manualChunks(id: string) {
                    if (id.includes("node_modules")) {
                        return "vender"
                    }
                },
                chunkFileNames: isProduction ? "assets/chunks/[name].[hash].js" : "assets/chunks/[name].js"
            }
        },
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true
            }
        },
        sourcemap: !isProduction,
        cssMinify: isProduction
    }
})
